package one.three.twentySix;

public class Remove {

	private static class Node<Item> {
		private Item item;
		private Node next;

		public Node<Item> remove(Node<Item> n, Item key) {
			Node<Item> c = n;

			while (c != null) {
				if (c.next != null && c.next.item.equals(key)) {
					c.next = c.next.next;
				}
				c = c.next;
			}

			if (n.item.equals(key)) {
				if (n.next == null) {
					return null;
				}
				Item i = (Item) n.next.item;
				Node<Item> nxt = n.next.next;
				n = new Node();
				n.item = i;
				n.next = nxt;
			}
			return n;
		}

		@Override
		public String toString() {

			return "[" + item + "]";
		}
	}

	public static void main(String[] args) {
		Node<String> f = new Node<String>();
		f.item = "0";
		Node<String> c = f;
		Node<String> five = null;
		for (int i = 1; i <= 10; i++) {
			c.next = new Node<String>();
			c.next.item = i + "";
			if (i == 5) {
				five = c.next;
			}
			c = c.next;
		}

		c = f;
		while (c != null) {
			System.out.println(c.item);
			c = c.next;
		}
		f=f.remove(f, "0");
		c = f;
		
		while (c != null) {
			System.out.println(c.item);
			c = c.next;
		}
	}

}
